package com.njitweb.controller;


import com.njitweb.pojo.Category;
import com.njitweb.service.CategoryService;
import com.njitweb.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping("/addCategory")
    public Result addCategory(@RequestBody Category category) {
        System.out.println(category);
        categoryService.addCategory(category);
        return Result.success("添加成功");
    }

    @GetMapping("/listCategory")
    public Result getAllCategory() {
        List<Category> results = categoryService.listCategory();
        return Result.success(results);
    }

    @DeleteMapping("/deleteCategory/{id}")
    public Result deleteCategory(@PathVariable Integer id) {
        categoryService.deleteCategory(id);
        return Result.success("删除成功");
    }

    @GetMapping("/getDetailCategory/{id}")
    public Result getCategory(@PathVariable Integer id) {
        Category category = categoryService.getDetailCategory(id);
        return Result.success(category);
    }

    @PutMapping("/updateCategory")
    public Result getCategory(@RequestBody Category category) {
        categoryService.updateCategory(category);
        return Result.success(category);
    }
}
